package com.eleme.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eleme.address.dto.AddressAddDTO;
import com.eleme.address.dto.AddressUpdateDTO;
import com.eleme.address.entity.Address;
import com.eleme.address.mapper.AddressMapper;
import com.eleme.address.service.AddressService;
import com.eleme.address.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public String addAddress(AddressAddDTO dto) {
        Address address = new Address();
        BeanUtils.copyProperties(dto, address);
        address.setAddressId(UUID.randomUUID().toString().replace("-", ""));
        if (address.getIsDefault() != null && address.getIsDefault() == 1) {
            // 取消用户其它默认地址
            QueryWrapper<Address> query = new QueryWrapper<>();
            query.eq("user_id", address.getUserId()).eq("is_default", 1);
            Address old = addressMapper.selectOne(query);
            if (old != null) {
                old.setIsDefault(0);
                addressMapper.updateById(old);
            }
        }
        addressMapper.insert(address);
        return "添加成功";
    }

    @Override
    public String updateAddress(AddressUpdateDTO dto) {
        if (!StringUtils.hasText(dto.getAddressId())) {
            throw new RuntimeException("地址ID不能为空");
        }
        Address address = addressMapper.selectById(dto.getAddressId());
        if (address == null) throw new RuntimeException("地址不存在");
        BeanUtils.copyProperties(dto, address);
        addressMapper.updateById(address);
        return "更新成功";
    }

    @Override
    public String removeAddress(String addressId) {
        int rows = addressMapper.deleteById(addressId);
        if (rows == 0) throw new RuntimeException("地址不存在");
        return "已删除";
    }

    @Override
    public String setDefault(String userId, String addressId) {
        // 取消用户其它默认地址
        QueryWrapper<Address> query = new QueryWrapper<>();
        query.eq("user_id", userId).eq("is_default", 1);
        Address old = addressMapper.selectOne(query);
        if (old != null) {
            old.setIsDefault(0);
            addressMapper.updateById(old);
        }
        Address address = addressMapper.selectById(addressId);
        if (address == null) throw new RuntimeException("地址不存在");
        address.setIsDefault(1);
        addressMapper.updateById(address);
        return "已设为默认地址";
    }

    @Override
    public List<AddressVO> listByUser(String userId) {
        QueryWrapper<Address> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        List<Address> list = addressMapper.selectList(query);
        List<AddressVO> res = new ArrayList<>();
        for (Address a : list) {
            AddressVO vo = new AddressVO();
            BeanUtils.copyProperties(a, vo);
            res.add(vo);
        }
        return res;
    }

    @Override
    public AddressVO getById(String addressId) {
        Address a = addressMapper.selectById(addressId);
        if (a == null) return null;
        AddressVO vo = new AddressVO();
        BeanUtils.copyProperties(a, vo);
        return vo;
    }
} 